<?php
/* -----------------------------------------------------------------
 *                        Aveghe My Blog
 * -----------------------------------------------------------------
 * Aveghe My Blog: made your blog easy!
 * Copyright (C) 2008 Elia Contini
 * 
 * Aveghe My Blog is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * Aveghe My Blog is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

class UserController extends Zend_Controller_Action
	{
		protected $_flashMessenger = null;
		protected $preferences = null;
		
		public function init()
			{
				// general settings
				$this->view->setEncoding('UTF-8');
				$this->view->addHelperPath('aveghe/views/helpers', 'Aveghe_View_Helper');
				
				// preferences
				$this->preferences = Zend_Registry::get('preferences');
				/*while(list($key, $value) = each($this->preferences))
							echo "$key : $value <br />"; die;*/
				
				// for report
				$this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
				$this->view->report = $this->_flashMessenger->getMessages();
			}

		public function loginRequiredAction()
			{
				// render login form
				$this->view->preferences = $this->preferences;
				$this->view->preferences['page_title'] = 'Login';
			}
			
		public function doLoginAction()
			{
				// disable autorendering for this action only
        		$this->_helper->viewRenderer->setNoRender();
        		
        		$request = $this->getRequest();
        		$username = $request->getParam('userLogin');
        		$password = md5($request->getParam('userPassword'));
        		
        		$user = new User();
        		$userInfo = $user->login($username, $password);
        		
        		if(isset($userInfo['user_login']))
        			{
        				$avegheNS = new Zend_Session_Namespace('avegheNS');
        				$avegheNS->userInfo = $userInfo;
        			}
        		else
        			$this->_flashMessenger->addMessage('<p class="warning">Username o password non validi.</p>'."\n");
        		
        		$this->_redirect('admin/');
			}
		
		public function doLogoutAction()
			{
				// disable autorendering for this action only
        		$this->_helper->viewRenderer->setNoRender();
        		
        		$avegheNS = new Zend_Session_Namespace('avegheNS');
        		Zend_Session::destroy();
        		
        		$this->_redirect('/admin/');
			}
		
		public function viewProfileAction()
			{
				$avegheNS = new Zend_Session_Namespace('avegheNS');
				
				if(isset($avegheNS->userInfo))
					{
						$this->view->userInfo = $avegheNS->userInfo;
						$this->view->preferences = $this->preferences;
						$this->view->preferences['page_title'] = 'Profilo di '.$avegheNS->userInfo['user_name'].' '.$avegheNS->userInfo['user_surname'];
					}
				else
					$this->_forward('./');
			}
		
		public function updateAvatarAction()
			{
				// disable autorendering for this action only
        		$this->_helper->viewRenderer->setNoRender();
        		
        		$avegheNS = new Zend_Session_Namespace('avegheNS');
        		
        		if($_FILES['userAvatar']['name'] != "")
	 				{
						
	 					if(!(ereg(".png$", $_FILES['userAvatar']['name']) || ereg(".gif$", $_FILES['userAvatar']['name']) || ereg(".jpg$", $_FILES['userAvatar']['name'])))  //check file extention
	    					;
	   					else if(file_exists(BASE_URL."img/".$_FILES['userAvatar']['name']))                      //file already exists?
	    					;
	   					else
	    					{
	    						copy($_FILES['userAvatar']['tmp_name'], 'img/'.$_FILES['userAvatar']['name']); //save file
	      						@unlink($_FILES['userAvatar']['tmp_name']);
	      						$user = new User();
	      						$user->updateAvatar($avegheNS->userInfo['user_id'], $_FILES['userAvatar']['name']);
	      						$avegheNS->userInfo = $user->getUserById($avegheNS->userInfo['user_id']);
	      						$this->_flashMessenger->addMessage('<p class="success">Immagine utente salvata con successo.</p>'."\n");
							}
	 				}
	 				
	 			$this->_redirect('./user/view-profile');
			}
		
		public function updateInfoAction()
			{
				// disable autorendering for this action only
        		$this->_helper->viewRenderer->setNoRender();
        		
        		$avegheNS = new Zend_Session_Namespace('avegheNS');
        		
        		$user = new User();
	      		$user->updateUserInfo( $avegheNS->userInfo['user_id'],
	      							   $_POST['userName'],
	      							   $_POST['userSurname'],
	      							   $_POST['userEmail'],
	      							   $_POST['userJabber']
	      							 );
	      		$avegheNS->userInfo = $user->getUserById($avegheNS->userInfo['user_id']);
	      		$this->_flashMessenger->addMessage('<p class="success">Informazioni utente salvate con successo.</p>'."\n");			 
	      		$this->_redirect('./user/view-profile');
			}
		
		public function updateDescriptionAction()
			{
				// disable autorendering for this action only
        		$this->_helper->viewRenderer->setNoRender();
        		
        		$avegheNS = new Zend_Session_Namespace('avegheNS');
        		
        		$user = new User();
	      		$user->updateUserDescription($avegheNS->userInfo['user_id'], $_POST['userDescription']);
	      		$avegheNS->userInfo = $user->getUserById($avegheNS->userInfo['user_id']);
	      		$this->_flashMessenger->addMessage('<p class="success">Descrizione utente salvata con successo.</p>'."\n");			 
	      		$this->_redirect('./user/view-profile');
			}
		
		public function updatePasswordAction()
			{
				// disable autorendering for this action only
        		$this->_helper->viewRenderer->setNoRender();
        		
        		$avegheNS = new Zend_Session_Namespace('avegheNS');
        		
        		if(isset($_POST['userPassword']) && $_POST['userPassword'] != '' && strlen($_POST['userPassword']) >= 8)
	        		{
		        		$password = md5($_POST['userPassword']);
		        		$passwordCheck = md5($_POST['userPasswordCheck']);
		        		
		        		if($password == $passwordCheck)
		        			{
		        				$user = new User();
		        				$user->updatePassword($avegheNS->userInfo['user_id'], $password);
			      				$avegheNS->userInfo = $user->getUserById($avegheNS->userInfo['user_id']);
			      				$this->_flashMessenger->addMessage('<p class="success">Password utente salvata con successo.</p>'."\n");
		        			}
		        		else
		        			$this->_flashMessenger->addMessage('<p class="warning">Le due password inserite non corrispondono.</p>'."\n");
	        		}
	        	else
	        		$this->_flashMessenger->addMessage('<p class="warning">La password deve essere lunga almeno otto caratteri.</p>'."\n");
	        		
	      		$this->_redirect('./user/view-profile');
			}
	}
?>